package org.gw.building.repositories;

import org.gw.building.model.CourseLeaveApply;
import org.gw.building.model.StuInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CourseLeaveApplyRepository extends JpaRepository<CourseLeaveApply, String> {

	List<CourseLeaveApply> findByCourseInfoIdInAndStuInfoUserUid(List<String> asList, String uid);

	@Query("from CourseLeaveApply t where (?1 is null or t.courseInfo.term.id=?1) and (?2 is null or t.courseInfo.id=?2) and (?3 is null or t.courseInfo.kclb=?3) and (?4 is null or t.stuInfo.user.name like ?4 or t.stuInfo.user.uid like ?4) and (?5 is null or t.stuInfo.user.department=?5) and (?6 is null or t.stuInfo.classInfo.id=?6) and"
			+ " (t.status =?7 or ?7 is null) and (?7 is not null or t.status in ?8)")
	Page<CourseLeaveApply> query(String term, String course, String type, String stuName, String department,
                                 String clazz, Integer status, List<Integer> list, Pageable pageable);

	List<CourseLeaveApply> findByCourseInfoIdAndStuInfoIdInAndStatus(String course, List<String> students,
                                                                     Integer status);

	CourseLeaveApply findByStatusAndStuInfo(int status, StuInfo stuInfo);

	CourseLeaveApply findByStatusInAndStuInfo(List<Integer> status, StuInfo stuInfo);
	@Query("select t.stuInfo.id ,count(t.id) from CourseLeaveApply t where t.stuInfo.id in ?1 and t.status=3 group by t.stuInfo.id")
	List<Object[]> queryCountByStuInfoIdIn(List<String> asList);
}
